#!/bin/bash
set -uo pipefail

declare -a ARCHS=("macplus" "ibmpc" "atarist")

pcejs_load_config () {
  if [[ ! -f ./pcejs_build_conf.sh ]]; then
    # no existing config file, copy from default
    cp ./scripts/pcejs_build_conf_default.sh ./pcejs_build_conf.sh
  fi

  set -a # all sourced vars are exported
  source ./pcejs_build_conf.sh
  set +a # restore default behaviour
}

pcejs_save_config () {
   (set -o posix; set) | grep "PCEJS_conf_" > ./pcejs_build_conf.sh
}

pcejs_print_config () {
  (set -o posix; set) | grep "PCEJS_"
}

pcejs_init_env_from_config () {
  # initialise build env from config
  # should be called after changing any PCEJS_conf var
  source ./scripts/pcejs_build_env.sh
  if [[ -z $PCEJS_EMSDK_PATH ]]; then
    echo "ERROR: could't resolve emsdk path from env or config"
    exit 1
  fi
}

# ensure cwd is project root dir
repo_root=$(git rev-parse --show-toplevel)
cd "$repo_root"


pcejs_load_config
pcejs_init_env_from_config

pcejs_build=$0
taskname=${1:-"default"}


echo "start $taskname ${2:-""}"
case "$taskname" in
  "env") # Print build environment variables
    pcejs_print_config
    ;;

  "init") # Create build config file (./pcejs_build_conf.sh) if none exists
    pcejs_save_config
    ;;

  "configure") # Run ./configure for emulator build
    platform=${2:-""}

    if [[ $platform == "native" ]]; then
      export PCEJS_conf_emscripten=""
      export PCEJS_conf_prefix="build-native/"
    fi
    if [[ $platform == "em" ]]; then
      export PCEJS_conf_emscripten="yes"
      export PCEJS_conf_prefix="build/"
    fi
    pcejs_init_env_from_config

    ./scripts/10-configure.sh

    pcejs_save_config
    ;;

  "make") # Compile emulator source to LLVM bitcode (clean and make)
    ./scripts/20-make.sh
    ;;

  "remake") # Recompile only changed files of emulator source to LLVM bitcode
    ./scripts/21-remake.sh
    ;;

  "clean") # Clean source tree
    cleanall=${2:-""}

    if [[ -n $cleanall ]]; then
      ./scripts/a0-clean.sh
    else
      make clean
    fi
    ;;

  "afterbuild") # Convert LLVM bitcode to JS
    target=${2:-""}

    if [[ -n $target ]]; then
      export PCEJS_conf_target=$target
    fi
    pcejs_init_env_from_config

    ./scripts/30-afterbuild.sh
    ;;

  "build") # Configure build and compile emulator to JS
    target_or_platform=${2:-""}


    if [[ $target_or_platform == "native" ]]; then
      $pcejs_build configure native
      $pcejs_build make
    else
      if [[ -n $target_or_platform ]]; then
        export PCEJS_conf_target=$target_or_platform
      fi
      pcejs_init_env_from_config
      $pcejs_build configure em
      $pcejs_build make
      $pcejs_build afterbuild "$PCEJS_conf_target"
    fi  

    pcejs_save_config
    ;;

  "rebuild") # Build last target again
    target_or_platform=${2:-""}

    if [[ $target_or_platform == "native" ]]; then
      $pcejs_build remake
    elif [[ -n $target_or_platform ]]; then
      export PCEJS_conf_target=$target_or_platform
      pcejs_init_env_from_config
      $pcejs_build remake
      $pcejs_build afterbuild "$PCEJS_conf_target"
    else
      $pcejs_build remake
      for arch in "${ARCHS[@]}"; do
        $pcejs_build afterbuild "$arch"
      done
    fi
    ;;

  "module") # Build commonjs module
    arch=${2:-""}

    echo "building commonjs module for $2"

    if [[ -n $arch ]]; then
      echo "building commonjs module for arch $arch";
      ./scripts/module_build.sh "$arch"
    else
      for arch in "${ARCHS[@]}"; do
      echo "building commonjs module for arch $arch";
        ./scripts/module_build.sh "$arch"
      done
    fi
    ;;

  "example") # run example
    arch=${2:-""}

    if [[ -n $arch ]]; then
      echo "running example for arch $arch";
    else
      echo "arch not specified";
      exit 1;
    fi

    ./example/run_example.sh "$arch"
    ;;

  "default")
    $pcejs_build configure em
    $pcejs_build make

    for arch in "${ARCHS[@]}"; do
      $pcejs_build afterbuild "$arch"
      $pcejs_build module "$arch"
    done
    ;;

  *)
    echo "unknown task: $taskname"
    exit 1
    ;;
esac

echo "end $taskname ${2:-""}"